﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ini编辑器CS
{
    public class MOD
    {
        public string name;
        public string owner;
        public string version;
        public string element;
        public string foldpath;
        public string filepath;
        public string readme;
        public CardPile cardPile;
        public MOD(string _filepath, string _foldpath)
        {
            Console.WriteLine("正在加载MOD");
            filepath = _filepath;
            foldpath = _foldpath;
                Load();
                display();
            Console.WriteLine("MOD加载完成");
        }
        public MOD(string _name,string _owner,string _version,string _element)
        {
            name = _name;
            owner = _owner;
            version = _version;
            element = _element;
            foldpath = Path.GetTempPath() + name;
            while (Directory.Exists(foldpath) == true)//如果不存在就创建file文件夹
            {
                foldpath += "0";
            }
            foldpath += "\\";
            cardPile = new CardPile(this);
        }
        void Load()
        {
            Loadini();
            cardPile = new CardPile(this);
        }
        public void Loadini()
        {
            StreamReader sr = new StreamReader(foldpath + "config.ini", Encoding.UTF8);
            while (sr.Peek()!=-1)
            {
                if (Convert.ToChar(sr.Peek()) == '[')
                    LoadSection(sr);
                else
                    sr.ReadLine();
            }
            sr.Close();
        }
        void LoadSection(StreamReader sr)
        {
            string str = sr.ReadLine();
            if (str == "[Info]")
                LoadInfo(sr);
        }
        void LoadInfo(StreamReader sr)
        {
            string s;
            while (sr.Peek()!=-1)
            {
               
                if (Convert.ToChar(sr.Peek()) == '[')
                {
                    LoadSection(sr);
                    break;
                }
                else
                {
                    s = sr.ReadLine();
                    string[] sArray = s.Split('=');
                    if (sArray[0] == "Name")
                        name = sArray[1];
                    else if (sArray[0] == "Owner")
                        owner = sArray[1];
                    else if (sArray[0] == "Version")
                        version = sArray[1];
                    else if (sArray[0] == "Element")
                        element = sArray[1];
                }  
            }
        }
        public void display()
        {
            Console.WriteLine(name);
            Console.WriteLine(owner);
            Console.WriteLine(version);
            Console.WriteLine(element);
        }
        public void Save()
        {
            if (Directory.Exists(foldpath) == false)//如果不存在就创建file文件夹
            {
                Directory.CreateDirectory(foldpath);
            }
            StreamWriter sw = new StreamWriter(foldpath + "\\config.ini", false);
            sw.WriteLine("[Info]");
            sw.WriteLine("Name="+name);
            sw.WriteLine("Owner="+owner);
            sw.WriteLine("Version="+version);
            sw.WriteLine("Element="+element);
            sw.Close();//写入
            cardPile.Save();
        }
    }
}
